<template>
	<view>
    <view class="goods_list">
      <view v-for="(goods, i) in goodsList" :key="i" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        queryInfo: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        // 商品列表数据
        goodsList: [],
        // 总数居条数
        total: '',
        // 节流阀
        isLoading: false
      }
		},
    onLoad(options) {
      this.queryInfo.cid = options.cid || '';
      this.queryInfo.query = options.query || '';
      this.getGoodsList();
    },
    // 上拉触底钩子函数
    onReachBottom() {
      // 判断数据是否加载完毕
      if(this.queryInfo.pagenum * this.queryInfo.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')
      // 节流设置
      if(this.isLoading) return
      
      this.queryInfo.pagenum ++
      this.getGoodsList()
    },
    // 下拉刷新钩子函数
    onPullDownRefresh() {
      // 重置参数
      this.queryInfo.pagenum = 1
      this.goodsList = []
      this.total = 0
      this.isLoading = false
      // 重新发起数据请求并关闭加载框
      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    methods: {
      async getGoodsList(cb) {
        // 打开节流阀
        this.isLoading = true
        
        const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryInfo)
        // 关闭节流阀
        this.isLoading = false
        // 只有数据请求完毕,再调用回调函数cb
        cb && cb()
        if(res.meta.status !== 200) return uni.$showMsg()
        // this.goodsList = res.message.goods
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    } 
	}
</script>

<style lang="scss">
</style>
